<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use ZigaStrgar\Orderable\Orderable;

/**
 * App\Building
 *
 * @property mixed $build_match_id
 * @mixin \Eloquent
 * @property int $id
 * @property string|null $slider 轮播图
 * @property string|null $area 面积
 * @property int|null $area_build_id 面积区域id
 * @property int|null $rent 租金
 * @property string|null $address 地址
 * @property int|null $park_id 园区id
 * @property string|null $is_company 是否注册公司：0否1是
 * @property string|null $use_rate 使用率
 * @property string|null $is_separate 是否分割：0否1是
 * @property string|null $renovation 装修：0为毛坯1为精装修
 * @property string|null $floor 所在楼层
 * @property string|null $contact 联系人
 * @property string|null $telephone 联系电话
 * @property string|null $is_owner 是否业主：0否1是
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null $code 编码
 * @property string|null $tag 标签
 * @property string|null $standard_height 标准层高
 * @property int|null $manage_cost 管理费用
 * @property string|null $is_recommend 是否推荐
 * @property string|null $title 标题
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereAreaBuildId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereBuildMatchId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereContact($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereFloor($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereIsCompany($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereIsOwner($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereIsRecommend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereIsSeparate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereManageCost($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereParkId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereRenovation($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereRent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereSlider($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereStandardHeight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereTag($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereTelephone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereUseRate($value)
 * @property string|null $p_district_id
 * @property string|null $p_city_id
 * @property string|null $p_province_id
 * @property string|null $logo 首图
 * @property string|null $latitude 经度
 * @property string|null $longitude 纬度
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereLatitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereLogo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereLongitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building wherePCityId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building wherePDistrictId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building wherePProvinceId($value)
 * @property string|null $desc
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Building whereDesc($value)
 */
class Building extends Model
{
    use Orderable;

    public function orderable(){
        return [
            //DESC,ASC
            //'id' => 'DESC',
            'created_at' => 'DESC',
            'id'
        ];
    }
    public static function boot()
    {
        parent::boot();

        static::saving(function ($model) {
            if($model->logo==''){
                $arr = explode(',',$model->slider);
                if(count($arr)>=1 && substr($model->slider,0,1)==','){
                    $model->logo = $arr[1];
                }else{
                    $model->logo = $arr[0];
                }
            }
            $area = $model->area;
            $area_id = 5;
            if($area<120){
                $area_id = 1;
            }else if($area<140){
                $area_id = 2;
            }else if($area<180){
                $area_id = 3;
            }else if($area<200){
                $area_id = 4;
            }
            $model->area_build_id = $area_id;
        });
    }
    //
    protected $table = "building";

    public function getBuildMatchIdAttribute($value)
    {
        return explode(',', $value);
    }

    public function setBuildMatchIdAttribute($value)
    {
        $this->attributes['build_match_id'] = implode(',', $value);
    }

    public static function getSelectOptions()
    {
        $options = \DB::table('building')->select('id','title as text')->get();
        $selectOption = [];
        foreach ($options as $option){
            $selectOption[$option->id] = $option->text;
        }
        return $selectOption;
    }
}
